﻿using EasyTest.Shared.TestAttribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ZLGCANoptimize
{
    public class TBOX_4G
    {
        //static ZLGCANHelper zLGCANHelper = null;
        //static List<ZCAN_Receive_Data> zCAN_Receive_Datas = null;
        [ExposedMethodAttribute("")]
        public bool 读4G模块ICCID信息B001(int 通道, out string 模块ICCID信息Hex, out string 模块ICCID信息Asc, out int 字节长度, out string 信息)
        {
            模块ICCID信息Hex = "";
            模块ICCID信息Asc = "";
            字节长度 = 0;
            信息 = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 300, 100);
                Thread.Sleep(50);
                bool r1 = false;//03 22 B0 01 00 00 00 00
                for (int i = 0; i < 5; i++)
                {
                    r1 = zLGCANHelper.SentData(通道, 0x761, "03 22 B0 01 00 00 00 00", true, SendType.正常发送);
                    Thread.Sleep(50);
                    zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                    if (r1)
                    {
                        break;
                    }
                }
                if (!r1)
                {
                    信息 = "发送失败...";
                    模块ICCID信息Hex = 信息;
                    模块ICCID信息Asc = 信息;
                    return false;
                }
                Thread.Sleep(50);
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);

                string[] hexValues = new string[20];
                int num = 0;
                bool ok10 = false;
                bool ok21 = false;
                bool ok22 = false;
                bool ok23 = false;
                /*
                    10 17 62 B0 01 01 20 20
                    21 20 20 20 20 20 20 20
                    22 20 20 20 20 20 20 20
                    23 20 20 20 AA AA AA AA
                */
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("22"))
                    {
                        ok22 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("23"))
                    {
                        ok23 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 3) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }
                字节长度 = hexValues.Length;
                var hexValues0 = "";
                if (!ok10 || !ok21 || !ok22 || !ok23)
                {
                    信息 = string.Join("", datas);
                    模块ICCID信息Hex = 信息;
                    模块ICCID信息Asc = 信息;
                    return false;
                }
                foreach (String hex in hexValues)
                {
                    int value = Convert.ToInt32(hex, 16);
                    string stringValue = Char.ConvertFromUtf32(value);
                    hexValues0 += stringValue;
                }
                信息 = string.Join(",", datas);
                模块ICCID信息Hex = string.Join("", hexValues);
                模块ICCID信息Asc = hexValues0;

                return ok10 && ok21 && ok22 && ok23;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                throw;
            }
        }
        [ExposedMethodAttribute("")]
        public bool 读4G模块IMEI信息B002(int 通道, out string 模块IMEI信息Hex, out string 模块IMEI信息Asc, out int 字节长度, out string 信息)
        {
            模块IMEI信息Hex = "";
            模块IMEI信息Asc = "";
            字节长度 = 0;
            信息 = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 300, 100);
                Thread.Sleep(50);
                bool r1 = false;
                for (int i = 0; i < 5; i++)
                {
                    r1 = zLGCANHelper.SentData(通道, 0x761, "03 22 B0 02 00 00 00 00", true, SendType.正常发送);
                    Thread.Sleep(50);
                    zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                    if (r1)
                    {
                        break;
                    }
                }
                if (!r1)
                {
                    信息 = "发送失败...";
                    模块IMEI信息Hex = 信息;
                    模块IMEI信息Asc = 信息;
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);

                string[] hexValues = new string[24];
                int num = 0;
                bool ok10 = false;
                bool ok21 = false;
                bool ok22 = false;
                bool ok23 = false;
                /*
                    10 1B 62 B0 02 01 20 20
                    21 20 20 20 20 20 20 20
                    22 20 20 20 20 20 20 20
                    23 20 20 20 20 20 20 20
                */
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("22"))
                    {
                        ok22 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("23"))
                    {
                        ok23 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }
                字节长度 = hexValues.Length;
                var hexValues0 = "";
                if (!ok10 || !ok21 || !ok22 || !ok23)
                {
                    信息 = string.Join("", datas);
                    模块IMEI信息Hex = 信息;
                    模块IMEI信息Asc = 信息;
                    return false;
                }
                foreach (String hex in hexValues)
                {
                    int value = Convert.ToInt32(hex, 16);
                    string stringValue = Char.ConvertFromUtf32(value);
                    hexValues0 += stringValue;
                }
                信息 = string.Join(",", datas);
                模块IMEI信息Hex = string.Join("", hexValues);
                模块IMEI信息Asc = hexValues0;

                return ok10 && ok21 && ok22 && ok23;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                throw;
            }
        }
        [ExposedMethodAttribute("")]
        public bool 读4G模块软件版本号B003(int 通道, out string 模块软件版本号Hex, out string 模块软件版本号Asc, out int 字节长度, out string 信息)
        {
            模块软件版本号Hex = "";
            模块软件版本号Asc = "";
            字节长度 = 0;
            信息 = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 300, 100);
                Thread.Sleep(50);
                bool r1 = false;
                for (int i = 0; i < 5; i++)
                {
                    r1 = zLGCANHelper.SentData(通道, 0x761, "03 22 B0 03 00 00 00 00", true, SendType.正常发送);
                    Thread.Sleep(50);
                    zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                    if (r1)
                    {
                        break;
                    }
                }
                if (!r1)
                {
                    信息 = "发送失败...";
                    模块软件版本号Hex = 信息;
                    模块软件版本号Asc = 信息;
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);

                string[] hexValues = new string[24];
                int num = 0;
                bool ok10 = false;
                bool ok21 = false;
                bool ok22 = false;
                bool ok23 = false;
                /*
                    10 1B 62 B0 03 01 20 20
                    21 20 20 20 20 20 20 20
                    22 20 20 20 20 20 20 20
                    23 20 20 20 20 20 20 20
                */
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("22"))
                    {
                        ok22 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("23"))
                    {
                        ok23 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }
                字节长度 = hexValues.Length;
                var hexValues0 = "";
                if (!ok10 || !ok21 || !ok22 || !ok23)
                {
                    信息 = string.Join("", datas);
                    模块软件版本号Hex = 信息;
                    模块软件版本号Asc = 信息;
                    return false;
                }
                foreach (String hex in hexValues)
                {
                    int value = Convert.ToInt32(hex, 16);
                    string stringValue = Char.ConvertFromUtf32(value);
                    hexValues0 += stringValue;
                }
                信息 = string.Join(",", datas);
                模块软件版本号Hex = string.Join("", hexValues);
                模块软件版本号Asc = hexValues0;

                return ok10 && ok21 && ok22 && ok23;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                throw;
            }
        }
        [ExposedMethodAttribute("")]
        public bool 读4G模块IMSI信息4C60(int 通道, out string 模块IMSI信息Hex, out string 模块IMSI信息Asc, out int 字节长度, out string 信息)
        {
            模块IMSI信息Hex = "";
            模块IMSI信息Asc = "";
            字节长度 = 0;
            信息 = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 300, 100);
                Thread.Sleep(50);
                bool r1 = false;
                for (int i = 0; i < 5; i++)
                {
                    r1 = zLGCANHelper.SentData(通道, 0x761, "03 22 4C 60 00 00 00 00", true, SendType.正常发送);
                    Thread.Sleep(50);
                    zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                    if (r1)
                    {
                        break;
                    }
                }
                if (!r1)
                {
                    信息 = "发送失败...";
                    模块IMSI信息Hex = 信息;
                    模块IMSI信息Asc = 信息;
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);

                string[] hexValues = new string[17];
                int num = 0;
                bool ok10 = false;
                bool ok21 = false;
                bool ok22 = false;
                /*
                    10 14 62 4C 60 01 20 20
                    21 20 20 20 20 20 20 20 
                    22 20 20 20 20 20 20 20 
                */
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("22"))
                    {
                        ok22 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }
                字节长度 = hexValues.Length;
                var hexValues0 = "";
                if (!ok10 || !ok21 || !ok22)
                {
                    信息 = string.Join("", datas);
                    模块IMSI信息Hex = 信息;
                    模块IMSI信息Asc = 信息;
                    return false;
                }
                foreach (String hex in hexValues)
                {
                    int value = Convert.ToInt32(hex, 16);
                    string stringValue = Char.ConvertFromUtf32(value);
                    hexValues0 += stringValue;
                }
                信息 = string.Join(",", datas);
                模块IMSI信息Hex = string.Join("", hexValues);
                模块IMSI信息Asc = hexValues0;

                return ok10 && ok21 && ok22;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                throw;
            }
        }
        [ExposedMethodAttribute("")]
        public bool 读4G模块MSISDN信息4CD0(int 通道, out string 模块MSISDN信息Hex, out string 模块MSISDN信息Asc, out int 字节长度, out string 信息)
        {
            模块MSISDN信息Hex = "";
            模块MSISDN信息Asc = "";
            字节长度 = 0;
            信息 = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 300, 100);
                Thread.Sleep(50);
                bool r1 = false;
                for (int i = 0; i < 5; i++)
                {
                    r1 = zLGCANHelper.SentData(通道, 0x761, "03 22 4C D0 00 00 00 00", true, SendType.正常发送);
                    Thread.Sleep(50);
                    zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                    if (r1)
                    {
                        break;
                    }
                }
                if (!r1)
                {
                    信息 = "发送失败...";
                    模块MSISDN信息Hex = 信息;
                    模块MSISDN信息Asc = 信息;
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);

                string[] hexValues = new string[13];
                int num = 0;
                bool ok10 = false;
                bool ok21 = false;
                bool ok22 = false;
                /*
                    10 10 62 4C D0 AB 2E 2C
                    21 20 20 20 20 20 20 20
                    22 20 20 20 AA AA AA AA
                */
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    if (s.Trim().StartsWith("22"))
                    {
                        ok22 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i < 1 || i > 3) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }
                字节长度 = hexValues.Length;
                var hexValues0 = "";
                if (!ok10 || !ok21 || !ok22)
                {
                    信息 = string.Join("", datas);
                    模块MSISDN信息Hex = 信息;
                    模块MSISDN信息Asc = 信息;
                    return false;
                }
                foreach (String hex in hexValues)
                {
                    int value = Convert.ToInt32(hex, 16);
                    string stringValue = Char.ConvertFromUtf32(value);
                    hexValues0 += stringValue;
                }
                信息 = string.Join(",", datas);
                模块MSISDN信息Hex = string.Join("", hexValues);
                模块MSISDN信息Asc = hexValues0;

                return ok10 && ok21 && ok22;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                throw;
            }
        }
    }
}
